//package com.qy.changemachine.api
//
//import okhttp3.OkHttpClient
//import okhttp3.logging.HttpLoggingInterceptor
//import retrofit2.Retrofit
//import retrofit2.converter.gson.GsonConverterFactory
//
//object RetrofitClient {
//
////    private const val BASE_URL = "http://192.168.1.194:4523/"
////    private const val BASE_URL = "http://192.168.1.29:9001/"
//    private const val BASE_URL = "http://47.97.109.54:9001 "
//
//    private val okHttpClient = OkHttpClient.Builder()
//        .addInterceptor(HttpLoggingInterceptor().apply {
//            level = HttpLoggingInterceptor.Level.BODY
//        })
//        .build()
//
//    private val retrofit = Retrofit.Builder()
//        .baseUrl(BASE_URL)
//        .client(okHttpClient)
//        .addConverterFactory(GsonConverterFactory.create())
//        .build()
//
//
//    // 下载接口的 Retrofit 实例（不打印响应体）
//    private val downloadOkHttpClient = OkHttpClient.Builder()
//        .addInterceptor(HttpLoggingInterceptor().apply {
//            level = HttpLoggingInterceptor.Level.HEADERS // 只打印请求头，不打印响应体
//        })
//        .build()
//
//    private val downloadRetrofit = Retrofit.Builder()
//        .baseUrl(BASE_URL)
//        .client(downloadOkHttpClient)
//        .addConverterFactory(GsonConverterFactory.create())
//        .build()
//
//    val downloadService: ApiService by lazy {
//        downloadRetrofit.create(ApiService::class.java)
//    }
//
//
//    val apiService: ApiService by lazy {
//        retrofit.create(ApiService::class.java)
//    }
//}

package com.qy.changemachine.api

import okhttp3.ConnectionPool
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException
import java.util.concurrent.TimeUnit

object RetrofitClient {

    // 基础 URL（确保没有多余的空格）
 private const val BASE_URL = "http://47.97.109.54:9001"
//    private const val BASE_URL = "http://192.168.1.61:9001"

    // 自定义拦截器：添加 Connection: close 请求头
    class NetInterceptor : Interceptor {
        @Throws(IOException::class)
        override fun intercept(chain: Interceptor.Chain): Response {
            val request = chain.request()
                .newBuilder()
                .addHeader("Connection", "close") // 添加 Connection: close 请求头
                .build()
            return chain.proceed(request)
        }
    }

    // 默认的 OkHttpClient（用于普通请求）
    private val okHttpClient = OkHttpClient.Builder()
        .addInterceptor(HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY // 打印请求和响应的详细信息
        })
        .addNetworkInterceptor(NetInterceptor()) // 添加网络拦截器
        .connectTimeout(30, TimeUnit.SECONDS) // 连接超时
        .readTimeout(30, TimeUnit.SECONDS) // 读取超时
        .writeTimeout(30, TimeUnit.SECONDS) // 写入超时
        .retryOnConnectionFailure(false) // 不自动重试连接失败
        .connectionPool(ConnectionPool(0, 1, TimeUnit.MILLISECONDS)) // 禁用连接池
        .build()

    // 默认的 Retrofit 实例
    private val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    // 下载接口的 OkHttpClient（不打印响应体）
    private val downloadOkHttpClient = OkHttpClient.Builder()
        .addInterceptor(HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.HEADERS // 只打印请求头，不打印响应体
        })
        .addNetworkInterceptor(NetInterceptor()) // 添加网络拦截器
        .connectTimeout(30, TimeUnit.SECONDS) // 连接超时
        .readTimeout(30, TimeUnit.SECONDS) // 读取超时
        .writeTimeout(30, TimeUnit.SECONDS) // 写入超时
        .retryOnConnectionFailure(false) // 不自动重试连接失败
        .connectionPool(ConnectionPool(0, 1, TimeUnit.MILLISECONDS)) // 禁用连接池
        .build()

    // 下载接口的 Retrofit 实例
    private val downloadRetrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(downloadOkHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    // 普通请求的服务接口
    val apiService: ApiService by lazy {
        retrofit.create(ApiService::class.java)
    }

    // 下载请求的服务接口
    val downloadService: ApiService by lazy {
        downloadRetrofit.create(ApiService::class.java)
    }
}